package A3JGroupsGC;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.blocks.ReplicatedHashMap;

import A3GCPackage.NodeRanker;
import A3JGroups.A3JGNode;
import A3JGroups.GenericRole;

public class GCGenericRole extends GenericRole
{
	// added by Tom 2012-06-04 - attempt to get free nodes into the GC at location join time
	protected NodeRanker   nodeRanker;
	protected JChannel     nodeRecyclerChannel;
	protected final String gcChannel = "A3GCPackageChannel";
	protected String       properties = null;

	public GCGenericRole(A3JGNode node, String groupName, JChannel chan,
			ReplicatedHashMap<String, Object> map) 
	{
		super(node, groupName, chan, map);
		
		try 
		{
			nodeRecyclerChannel = new JChannel(properties);
			nodeRanker = new NodeRanker(nodeRecyclerChannel);
			nodeRecyclerChannel.connect("A3GCPackageChannel");
			
			// can't add node here
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	@Override
	public void waitElection()
	{
		// lets add this node to the system
		// 		groupName should be empty because this guys is not doing anything
		System.out.println("GenericRole[" + chan.getAddressAsString() + "]: added channel to noderanker.");
		nodeRanker.addNodeIfNotThere(chan.getAddress().toString(), "", chan.getAddress());
		// call the super
		super.waitElection();
	}
}
